<?php

namespace App\Traits;

use GuzzleHttp\Client as Http;

trait BaseHttp
{

    //请求头信息
    private $headers;

    //期望返回数据类型
    private $expectResponseType = 'json';

    //设置期望返回数据类型
    protected function setExpectResponseType($type)
    {
        $this->expectResponseType = $type;
    }

    /**
     * get 请求
     *
     * @param  string      $url    URL
     * @param  array       $params 参数
     * @return array|mixed
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function httpGet($url, $params = [])
    {
        return $this->request('get', $url, $params);
    }

    /**
     * post 请求
     *
     * @param  string      $url    URL
     * @param  array       $params 参数
     * @param  string      $type   请求参数类型
     * @return array|mixed
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function httpPost($url, $params = [], $type = 'query')
    {
        return $this->request('post', $url, $params, $type);
    }

    /**
     * put 请求
     *
     * @param  string      $url    URL
     * @param  array       $params 参数
     * @param  string      $type   请求参数类型
     * @return array|mixed
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function httpPut($url, $params = [], $type = 'query')
    {
        return $this->request('put', $url, $params, $type);
    }

    /**
     * delete 请求
     *
     * @param  string      $url    URL
     * @param  array       $params 参数
     * @return array|mixed
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function httpDelete($url, $params = [])
    {
        return $this->request('delete', $url, $params);
    }

    /**
     * 封装http请求
     *
     * @param  string      $method 请求类型
     * @param  string      $url    URL
     * @param  array       $params 参数
     * @param  string      $type   请求参数类型
     * @return array|mixed
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function request($method, $url, $params = [], $type = 'query')
    {
        $url = trim($url);
        $retData = ['code' => 0, 'msg' => '', 'data' => []];

        $type = $type ?: 'query';

        $options = [];

        $options[$type] = $params;

        if ($this->headers) {
            $options['headers'] = $this->headers;
        }

        try {
            $http = new Http(['connect_timeout' => 5, 'timeout' => 10]);
            $response = $http->request($method, $url, $options);
        } catch (\Exception $e) {
            $retData['code'] = $e->getCode();
            $retData['msg'] = $e->getMessage();

            return $retData;
        }
        $body = $response->getBody();
        $ret = $body->getContents();
        if (! $ret) {
            return $retData;
        }
        if ($this->expectResponseType != 'json') {
            return $ret;
        }

        $arr = json_decode($ret, 1);
        if (! is_array($arr)) {
            return $ret;
        }
        dd($arr);
        $retData['code'] = $arr['errcode'];
        $retData['msg'] = $retData['code'] == 0 ? 'success' : '';
        $retData['data'] = $arr;

        return $retData;
    }

    /**
     * 设置请求头
     *
     * @param $headers
     */
    protected function setHeaders($headers)
    {
        $this->headers = $headers;
    }

    /**
     * 添加请求头
     *
     * @param string $name
     * @param string $val
     */
    protected function addHeader($name, $val)
    {
        $this->headers[$name] = $val;
    }
}
